{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "0",
   "metadata": {},
   "source": [
    "# Buckaroo works in Solara too"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "1",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "import solara\n",
    "from buckaroo.solara_buckaroo import SolaraDFViewer"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'a':[10,20]})\n",
    "@solara.component\n",
    "def Page():\n",
    "    bw = SolaraDFViewer(df=df)\n",
    "display(Page())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3",
   "metadata": {},
   "source": [
    "## You can turn off pinned_rows too"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "4",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "df = pd.DataFrame({'a':[10,20]})\n",
    "@solara.component\n",
    "def Page():\n",
    "    bw = SolaraDFViewer(df=df, pinned_rows=[])\n",
    "display(Page())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5",
   "metadata": {},
   "source": [
    "# And it works with Polars"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "import polars as pl\n",
    "from buckaroo.solara_buckaroo import SolaraPolarsDFViewer\n",
    "pldf = pl.DataFrame({'a':[10,20]})\n",
    "@solara.component\n",
    "def Page():\n",
    "    bw = SolaraPolarsDFViewer(df=pldf, pinned_rows=[])\n",
    "display(Page())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7",
   "metadata": {},
   "source": [
    "# Let's look at more data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "8",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "cb_trips_df = pd.read_csv(\"https://github.com/paddymul/buckaroo-data/raw/main/cb_data/2016-04.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "9",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "@solara.component\n",
    "def Page():\n",
    "    bw = SolaraDFViewer(df=cb_trips_df, pinned_rows=[])\n",
    "display(Page())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10",
   "metadata": {},
   "source": [
    "# Building an app around SolaraDFViewer\n",
    "The following app hardcodes two dataframes and lets you select between them"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "11",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataframe_names = [\"foo\", \"bar\"]\n",
    "df_name = solara.reactive(\"foo\")\n",
    "\n",
    "dataframes = dict(foo=df,\n",
    "                  bar=pd.DataFrame({'c':[20,50,90], 'd':[False, True, True]}))\n",
    "\n",
    "@solara.component\n",
    "def Page():\n",
    "    solara.Select(label=\"df_name\", value=df_name, values=dataframe_names)\n",
    "    active_df = dataframes[df_name.value]\n",
    "    bw = SolaraDFViewer(active_df)\n",
    "\n",
    "    solara.Markdown(f\"**Selected**: {df_name.value}\")\n",
    "display(Page())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "12",
   "metadata": {},
   "source": [
    "# Reading large dataframes"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "13",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "url_base = \"https://github.com/paddymul/buckaroo-data/raw/main/cb_data/\"\n",
    "\n",
    "dataframe_names = [\n",
    "    \"2016-01.parq\", \"2016-02.parq\",\n",
    "    \"2016-03.parq\", \"2016-04.parq\"]\n",
    "\n",
    "df_name = solara.reactive(\"2016-01.parq\")\n",
    "\n",
    "@solara.component\n",
    "def Page():\n",
    "    solara.Select(label=\"df_name\", value=df_name, values=dataframe_names)\n",
    "\n",
    "    @solara.lab.use_task(dependencies=[df_name.value])\n",
    "    def read_df():\n",
    "        return pl.read_parquet(url_base+df_name.value)\n",
    "\n",
    "    solara.ProgressLinear(read_df.pending)\n",
    "    if read_df.latest is not None:\n",
    "        with solara.Div(style={\"opacity\": 0.3 if read_df.pending else 1.0}):\n",
    "            SolaraPolarsDFViewer(read_df.latest)\n",
    "\n",
    "    solara.Markdown(f\"**Selected**: {df_name.value}\")\n",
    "display(Page())"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
